package com.xydtech.ecard.imp.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.common.utils.sql.SqlUtil;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.imp.domain.EcardApiMerchantImportTask;
import com.xydtech.ecard.imp.domain.EcardMerchantImportHandover;
import com.xydtech.ecard.imp.service.IEcardApiMerchantImportTaskService;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysWechatApplet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
 * 导入任务接口
 * @author lin
 * @version 1.0
 * @create 2024/11/28 8:57
 */
@Controller
@Slf4j
@Api(tags = "电子名片-导入任务接口")
@RequestMapping("/ecardSaasApi/imp/merchantImport")
public class EcardApiMerchantImportController extends EcardApiBaseController {
    @Autowired
    private IEcardApiUserService ecardApiUserService;
    @Autowired
    private IEcardApiMerchantImportTaskService ecardApiMerchantImportTaskService;

    @PostMapping(value = "/getMerchantImportTaskList")
    @ApiOperation(value = "电子名片-导入任务列表接口", notes = "电子名片-导入任务列表接口")
    @ResponseBody
    @RsaRequest
    public ResponseModel getMerchantImportTaskList(@RequestBody String json) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前小程序id
            SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
            JSONObject jsonObject = JSONObject.parseObject(json);
            int pageNum = jsonObject.getInteger("pageNum");
            int pageSize = jsonObject.getInteger("pageSize");
            String orderByStr = jsonObject.getString("orderBy");
            String merchantType = jsonObject.getString("merchantType");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
            String search = jsonObject.getString("search");//查询条件姓名和手机号拆开
            String phone = jsonObject.getString("phone");
            String orderBy = SqlUtil.escapeOrderBySql(orderByStr);
            PageHelper.startPage(pageNum, pageSize, orderBy);
            List<EcardApiMerchantImportTask> merchantReservationTaskList = ecardApiMerchantImportTaskService.getMerchantImportTaskList(Long.valueOf(currentUserId), merchantType, search, wechatApplet.getDeptId(), phone, salt);
            return ResponseModel.okToJSONArray(new PageInfo(merchantReservationTaskList).getTotal(), merchantReservationTaskList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 导入任务转交(公用)
     *
     * @param ecardMerchantImportHandover
     * @return
     */
    @PostMapping(value = "/doTransfer")
    @ApiOperation(value = "电子名片-个人/商户/企业/新型农业主体导入任务转交接口", notes = "电子名片-个人/商户/企业/新型农业主体导入任务转交接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel doTransfer(@RequestBody EcardMerchantImportHandover ecardMerchantImportHandover) {
        try {
            String salt = getSalt();//盐值
            String currentUserId = getCurrentUserId();
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
            //当前登录人userId
            ecardMerchantImportHandover.setTransferId(Long.valueOf(currentUserId));
            ecardMerchantImportHandover.setTransferDeptId(userInfo.getDeptId());
            int updateCount = ecardApiMerchantImportTaskService.addMerchantImportTransferInfo(ecardMerchantImportHandover, getSalt());
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("转交失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }

    /**
     * 关闭导入任务(直接关闭，不走流程)
     * @param json
     * @return
     */
    @PostMapping(value = "/closeMerchantImportTask")
    @ApiOperation(value = "电子名片-关闭导入任务", notes = "电子名片-关闭导入任务")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel closeMerchantImportTask(@RequestBody String json) {

        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            Long taskId = jsonObject.getLong("taskId");
            if (StringUtils.objIsNullOrEmpty(taskId)) {
                return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
            }
            //个体工商户停用/任务停用
            int updateCount = ecardApiMerchantImportTaskService.closeMerchantImportTask(taskId);
            return updateCount > 0 ? ResponseModel.ok() : ResponseModel.error("删除失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }
}
